package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jinzhi.api.exception.IFastApiException;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.type.ApiEnumError;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.TrackDrawDO;
import com.jinzhi.jzweb.domain.TrackDrawDownLoadDO;
import com.jinzhi.jzweb.pojo.TrackDrawUnitNameDto;
import com.jinzhi.jzweb.service.TrackDrawService;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * <pre>
 * 赛道抽签表（存储抽签信息）
 * </pre>
 * <small> 2024-02-28 09:32:18 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/trackDraw")
public class TrackDrawController extends BaseController {
	@Autowired
	private TrackDrawService trackDrawService;

	@GetMapping()
	@RequiresPermissions("jzweb:trackDraw:list")
	String TrackDraw(){
	    return "jzweb/trackDraw/trackDraw";
	}

	/**
	 * 抽签接口
	 * @param trackDrawDTO
	 * @return
	 */
	@ResponseBody
	@GetMapping("/list")
	public Result<List<TrackDrawDO>> list(TrackDrawDO trackDrawDTO){

		return trackDrawService.trackDrawList(trackDrawDTO);
	}

	/**
	 * 查询上次的抽签结果
	 */
	@ResponseBody
	@GetMapping("/trackList")
	public Result<List<TrackDrawDO>> trackList(TrackDrawDO trackDrawDTO){

		return trackDrawService.trackList(trackDrawDTO);
	}

	/**
	 * 下载导入模板
	 * @param request
	 * @param respons
	 * @throws Exception
	 */
	@GetMapping("/exportTrackDraw")
	@ResponseBody
	public void exportListNum2(HttpServletRequest request, HttpServletResponse respons) throws Exception{
		List<TrackDrawUnitNameDto> list = new LinkedList<>();
		//导出操作
		EasyPoiUtil.exportExcel(list, "大赛抽签导入模版", "大赛抽签导入模版", TrackDrawUnitNameDto.class, "大赛抽签导入模版", respons);
	}


	/**
	 * 导入抽签参赛学校信息
	 * @param file
	 * @return
	 */
	@ResponseBody
	@PostMapping(value = "/importExcelTrackDraw")
	public Result<Object> importExcelTrackDraw(@RequestParam("file") MultipartFile file,Long cbatchId){
		return trackDrawService.importExcelTrackDraw(file,cbatchId);
	}


	/**
	 * 导出抽签结果信息
	 *
	 * @param response
	 */
	@Log("导出抽签结果信息")
	@RequestMapping("/downloadImportTemplate")
	@ResponseBody
	public Result downloadImportTemplate(HttpServletRequest request, HttpServletResponse response, TrackDrawDO trackDrawDTO) {
		if (trackDrawDTO.getCbatchId()==null){
			return Result.fail("请先选择要导出的批次！");
		}
		// 先校验是否进行了抽签
		Wrapper<TrackDrawDO> wrapper = new EntityWrapper<>();
		wrapper.eq("cbatch_id",trackDrawDTO.getCbatchId());
		//wrapper.orderBy(true,"record_number");
		wrapper.orderBy(true,"record_number_sort");
		// 查询数据库中的抽签表
		List<TrackDrawDO> trackDrawDOS = trackDrawService.selectList(wrapper);
		// 将查询出的数据转换成 4*18
		List<TrackDrawDO> setTemplateTrackDrawDOSList = trackDrawService.setTemplate(trackDrawDOS);
		// 导出集合
		List<TrackDrawDownLoadDO> trackDrawDownLoadDOS = new ArrayList<>();
		if (setTemplateTrackDrawDOSList != null && setTemplateTrackDrawDOSList.size() > 0) {
			// 循环遍历 转换数据
			for (TrackDrawDO parentTrackDraw : setTemplateTrackDrawDOSList) {
				if (parentTrackDraw.getTrackDrawDOList() != null && parentTrackDraw.getTrackDrawDOList().size() > 0) {
					// 导出对象
					TrackDrawDownLoadDO trackDrawDownLoadDO = new TrackDrawDownLoadDO();
					// 每行的数据量
					int listSize = parentTrackDraw.getTrackDrawDOList().size();
					// 第一列数据
					trackDrawDownLoadDO.setUnitName(listSize > 0 ? parentTrackDraw.getTrackDrawDOList().get(0).getUnitName() : "");
					trackDrawDownLoadDO.setRecordNumber(listSize > 0 ? parentTrackDraw.getTrackDrawDOList().get(0).getRecordNumber() : "");
					// 判断一行数据是否是两个
					if (listSize > 1) {
						trackDrawDownLoadDO.setUnitName2(parentTrackDraw.getTrackDrawDOList().get(1).getUnitName());
						trackDrawDownLoadDO.setRecordNumber2(parentTrackDraw.getTrackDrawDOList().get(1).getRecordNumber());
					}
					// 判断一行数据是否是三个
					if (listSize > 2) {
						trackDrawDownLoadDO.setUnitName3(parentTrackDraw.getTrackDrawDOList().get(2).getUnitName());
						trackDrawDownLoadDO.setRecordNumber3(parentTrackDraw.getTrackDrawDOList().get(2).getRecordNumber());
					}
					// 判断一行数据是否是四个
					if (listSize > 3) {
						trackDrawDownLoadDO.setUnitName4(parentTrackDraw.getTrackDrawDOList().get(3).getUnitName());
						trackDrawDownLoadDO.setRecordNumber4(parentTrackDraw.getTrackDrawDOList().get(3).getRecordNumber());
					}
					// 添加到导出集合
					trackDrawDownLoadDOS.add(trackDrawDownLoadDO);
				}
			}
				//导出操作
				EasyPoiUtil.exportExcel(trackDrawDownLoadDOS, "大赛抽签", "大赛抽签", TrackDrawDownLoadDO.class,  "大赛抽签", response);
		} else {
			throw new IFastApiException(ApiEnumError.NOTBATCHSTU.getCodeStr());
		}
		return Result.ok();
	}
}
